# Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
# Copyright: (c) <spug.dev@gmail.com>
# Released under the AGPL-3.0 License.
from django.core.paginator import Paginator
from django.views.generic import View
from django.db.models import F
from libs import json_response
from apps.account.models import History


class HistoryView(View):
    def get(self, request):
        histories = []
        history_list = History.objects.annotate(nickname=F('user__nickname'))
        pagesize = request.GET.get('pageSize')
        # 每页显示 1 篇文章
        paginator = Paginator(history_list, pagesize)
        # 获取 url 中的页码
        page = request.GET.get('pageNumber')
        # 将导航对象相应的页码内容返回给 articles
        history_lists = paginator.get_page(page)
        page_num = paginator.num_pages
        for item in history_lists:
            histories.append({
                'nickname': item.nickname,
                'ip': item.ip,
                'created_at': item.created_at.split('-', 1)[1],
                'total': page_num
            })
        return json_response(histories)
